import wx from "weixin-js-sdk";
import { Dialog } from "vant";
import { getJSSDKSign } from "@/api/index";
import { PAGE_BASE_URL } from "@/config";

export async function initJsSDK() {
  try {
    const data = await getJSSDKSign();
    const { appId, timestamp, nonceStr, signature } = data;
    wx.config({
      debug: false, // 开启调试模式
      appId, //公众号的唯一标识
      timestamp, //生成签名的时间戳
      nonceStr, //生成签名的随机串
      signature, //签名
      jsApiList: [
        //需要使用的JS接口列表
        "onMenuShareTimeline",
        "onMenuShareAppMessage",
        "onMenuShareQQ",
        "onMenuShareWeibo",
        "onMenuShareQZone",
        "chooseWXPay",
        "getLocation",
        "openLocation",
        "scanQRCode",
        "chooseImage",
        "uploadImage",
        "previewImage",
        "getLocalImgData",
        "startRecord",
        "stopRecord",
        "onVoiceRecordEnd",
        "uploadVoice"
      ]
    });

    wx.ready(() => {
      setShare({});
    });

    wx.error(res => {
      console.log("wx.error: ", res);
    });
  } catch (e) {
	console.log(e)
  }
}

export function setShare({
  title = "未来科学城", // 分享标题
  desc = "您的好友正在使用未来科学城园区服务平台，快去看看吧~",
  link = PAGE_BASE_URL, // 默认是首页
  imgUrl =PAGE_BASE_URL + "/img/logo.jpg", // 默认分享图标
}) {
  const infoThunk = str => obj =>
    console.log(`${str} + ${link} + ${JSON.stringify(obj)}`);
 
  wx.onMenuShareAppMessage({
    title,
    link,
    imgUrl,
    desc,
    cancel: infoThunk("onMenuShareAppMessage cancel"),
    fail: infoThunk("onMenuShareAppMessage fail"),
    success: infoThunk("onMenuShareAppMessage success")
  });

  wx.onMenuShareTimeline({
    title,
    link,
    imgUrl,
    desc,
    cancel: infoThunk("onMenuShareTimeline cancel"),
    fail: infoThunk("onMenuShareTimeline fail"),
    success: infoThunk("onMenuShareTimeline success")
  });
}
